<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 14:53
 */

namespace Admin\Model;


use Think\Model;
use Think\Upload;

class DocModel extends Model
{
	public function addData($post, $file)
	{
//		定义配置
		$cfg = array('rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH,
		);
//		dump($cfg['rootPath']) ;die;// "F:/Phplcoate/oa/Public/Upload/"
		/*
		 * array(9) {
  ["name"] => string(19) "绑定并登陆.png"
  ["type"] => string(9) "image/png"
  ["size"] => int(6840)
  ["key"] => int(0)
  ["ext"] => string(3) "png"
  ["md5"] => string(32) "426f455a5464b3d071420b7f3ca85882"
  ["sha1"] => string(40) "277c63cd41b9344ba78b06e31f7043747d55b771"
  ["savename"] => string(17) "5ad6ef0bb8fc4.png"
  ["savepath"] => string(11) "2018-04-18/"
}
		 * */
		$upload = new Upload($cfg);
		$info = $upload->uploadOne($file);
//		判断是否上传成功
//		dump($info);
//		die();
		if ($info) {
			$post['filepath'] = UPLOAD_ROOT_PATH . $info['savepath'] . $info['savename'];
			$post['filename'] = $info['savename'];
			$post['hasfile'] = 1;
			$post['addtime'] = time();
		}
		return $this->add($post);
	}

	public function edit($post, $file)
	{
		//		定义配置
		$cfg = array('rootPath' => WORKING_PATH . UPLOAD_ROOT_PATH,
		);
//		dump($cfg['rootPath']) ;die;// "F:/Phplcoate/oa/Public/Upload/"
		/*
		 * array(9) {
  ["name"] => string(19) "绑定并登陆.png"
  ["type"] => string(9) "image/png"
  ["size"] => int(6840)
  ["key"] => int(0)
  ["ext"] => string(3) "png"
  ["md5"] => string(32) "426f455a5464b3d071420b7f3ca85882"
  ["sha1"] => string(40) "277c63cd41b9344ba78b06e31f7043747d55b771"
  ["savename"] => string(17) "5ad6ef0bb8fc4.png"
  ["savepath"] => string(11) "2018-04-18/"
}
		 * */

		if ($file['error'] == 0) {
//			清除掉之前上传的文件信息  filepath
			$upload = new Upload($cfg);
//dump($post['filepath']);die;	清除掉之前上传的文件信息  filepath
			if (file_exists(WORKING_PATH . $post['filepath'])) {
//				dump($post['filepath']);die;5ad6f3febfd24
				unlink(WORKING_PATH . $post['filepath']);
			}
			$info = $upload->uploadOne($file);
//		判断是否上传成功
			if ($info) {
				$post['filepath'] = UPLOAD_ROOT_PATH . $info['savepath'] . $info['savename'];
				$post['filename'] = $info['savename'];
				$post['hasfile'] = 1;
				$post['addtime'] = time();
			}

		}
		return $this->save($post);
	}
}